﻿// cloudfunctions/getInteractionMessages/index.js
'use strict';
const db = uniCloud.database();

exports.main = async (event, context) => {
  const { receiverId } = event;

  // 校验参数
  if (!receiverId) {
    return { success: false, message: '接收者ID缺失' };
  }

  try {
    // 查询接收者为 receiverId 的互动消息，按时间倒序
    const res = await db.collection('interaction-messages')
      .where({ receiver_id: receiverId })
      .orderBy('create_time', 'desc')
      .get();

    return { success: true, data: res.data, message: '查询成功' };
  } catch (e) {
    console.error('getInteractionMessages 异常:', e);
    return { success: false, message: '查询失败：' + e.message };
  }
};